package mil.af.amc.adpetracker.web.helpers;

import mil.af.amc.adpetracker.model.*;
import mil.af.amc.adpetracker.model.comparators.EquipmentItemManufacturerComparator;
import mil.af.amc.adpetracker.model.comparators.EquipmentItemSerialComparator;
import mil.af.amc.adpetracker.model.comparators.UserDisplaynameComparator;
import mil.af.amc.adpetracker.service.AdpeAccountService;
import mil.af.amc.adpetracker.service.DraService;
import mil.af.amc.adpetracker.service.EquipmentItemService;
import mil.af.amc.adpetracker.service.UserService;
import mil.af.amc.adpetracker.service.exceptions.DraNotFoundException;
import mil.af.amc.radtaglibrary.tags.PageContextAwareBean;
import org.springframework.security.context.SecurityContext;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Viewhelper class for displaying AdpeAccount related information
 *
 * @author TSgt Jason Ferguson
 */
@Service("adpeAccountViewHelper")
public class AdpeAccountViewHelper implements PageContextAwareBean {

    private AdpeAccountService adpeAccountService;

    @Autowired
    public void setAdpeAccountService(AdpeAccountService adpeAccountService) {
        this.adpeAccountService = adpeAccountService;
    }

    private EquipmentItemService equipmentItemService;

    @Autowired
    public void setEquipmentItemService(EquipmentItemService equipmentItemService) {
        this.equipmentItemService = equipmentItemService;
    }

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    private DraService draService;

    @Autowired
    public void setDraService(DraService draService) {
        this.draService = draService;
    }

    private PageContext pageContext;

    @Autowired
    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    private int accountId;

    public void setAccountId(int accountId) {
        this.accountId = accountId;
    }

    private int userId;

    public void setUserId(int userId) {
        this.userId = userId;
    }

    private AdpeAccount adpeAccount;

    public void setAdpeAccount(AdpeAccount adpeAccount) {
        this.adpeAccount = adpeAccount;
    }

    public AdpeAccount getAdpeAccount() {
        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int adpeAccountId = (Integer) httpServletRequest.getSession().getAttribute("accountId");

        AdpeAccount acct = adpeAccountService.getById(adpeAccountId);

        return acct;
    }

    public List<AdpeAccount> getAll() {

        return adpeAccountService.getAll();
    }

    public List<AdpeAccount> getAllAccounts() {

        return adpeAccountService.getAll();
    }

    public List<AdpeAccount> getDraAccounts() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int draId = (Integer) httpServletRequest.getSession().getAttribute("draId");
        //int draId = (Integer) pageContext.getRequest().getAttribute("draId");
        DRA dra = null;
        try {
            dra = draService.getById(draId);
        } catch (DraNotFoundException e) {

        }

        List<AdpeAccount> accounts = dra.getAdpeAccounts();
        Collections.sort(accounts);
        return accounts;

    }

    public AdpeAccount getById() {

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        int id = (Integer) request.getSession().getAttribute("accountId");
        return adpeAccountService.getById(id);

    }

    public List<User> getCustodians() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int id = (Integer) httpServletRequest.getSession().getAttribute("accountId");

        AdpeAccount adpeAccount = adpeAccountService.getById(id);
        List<User> transientCustodianList = new ArrayList<User>();
        
        for (User u: adpeAccount.getPrimaryCustodian()) {
            transientCustodianList.add(u);
        }

        for (User u: adpeAccount.getAlternateCustodians()) {
            transientCustodianList.add(u);
        }

        return transientCustodianList;
    }

    public List<User> getPrimaryCustodians() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int id = (Integer) httpServletRequest.getSession().getAttribute("accountId");
        return new ArrayList<User>(adpeAccountService.getById(id).getPrimaryCustodian());
    }

    public List<User> getAlternateCustodians() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int id = (Integer) httpServletRequest.getSession().getAttribute("accountId");
        AdpeAccount adpeAccount = adpeAccountService.getById(id);
        List<User> alternateCustodianList = new ArrayList<User>(adpeAccount.getAlternateCustodians());
        Collections.sort(alternateCustodianList, new UserDisplaynameComparator());
        return alternateCustodianList;

    }

    public List<EquipmentItem> getEquipmentList() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int id = (Integer) httpServletRequest.getSession().getAttribute("accountId");

        AdpeAccount adpeAccount = adpeAccountService.getById(id);

        List<EquipmentItem> itemList = new ArrayList<EquipmentItem>(adpeAccount.getAccountInventory());
        Collections.sort(itemList, new EquipmentItemSerialComparator());
        Collections.sort(itemList, new EquipmentItemManufacturerComparator());

        return itemList;
    }

    public List<EquipmentItem> getPartialEquipmentItemListByAccount() {

        throw new UnsupportedOperationException();
    }

    public List<AdpeAccount> getAdpeAccountsByAccount() {

        SecurityContext securityContext = SecurityContextHolder.getContext();
        Account acct = (Account) securityContext.getAuthentication().getPrincipal();

        List<AccountAdpeAccount> aaa = new ArrayList<AccountAdpeAccount>(acct.getAccountAdpeAccounts());
        List<AdpeAccount> results = new ArrayList<AdpeAccount>();
        for (AccountAdpeAccount a: aaa) {
            results.add(a.getAdpeAccount());
        }

        return results;
    }

//    public List<AdpeAccount> getAdpeAccountsByUser() {
//
//        SecurityContext securityContext = SecurityContextHolder.getContext();
//        String username = ((org.springframework.security.userdetails.User) securityContext.getAuthentication().getPrincipal()).getUsername();
//        User user = userService.getUserByUsername(username);
//
//        List<AdpeAccount> accountList = userService.getUserAdpeAccounts(user);
//        Collections.sort(accountList);
//        return accountList;
//    }

    public int getAccountItemCount() {

        AdpeAccount acct = (AdpeAccount) pageContext.getAttribute("adpeAccount");
        return acct.getAccountInventory().size();

    }

    public double getAccountItemValue() {

        AdpeAccount acct = (AdpeAccount) pageContext.getAttribute("adpeAccount");
        return adpeAccountService.getAccountValue(acct.getId());

    }

    public int getItemCount() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int id = (Integer) httpServletRequest.getSession().getAttribute("accountId");
        return adpeAccountService.getEquipmentItemCount(id);
    }

    public double getAccountValue() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int id = (Integer) httpServletRequest.getSession().getAttribute("accountId");
        double accountValue = adpeAccountService.getAccountValue(id);

        return accountValue;

    }

/*
    public double getAccountValue() {

        int id = (Integer) pageContext.getRequest().getAttribute("accountId");

    }
*/

/*    public List<AccountFile> getFiles() {

        int id = (Integer) pageContext.getRequest().getAttribute("accountId");
        AdpeAccount account = adpeAccountService.getById(id);

        return account.getFiles();
    }*/
}
